using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace Elte.SqlSpIdx
{
    public class DBObjectBase
    {
        protected SqlConnection databaseConnection;
        protected SqlTransaction databaseTransaction;

        public DBObjectBase()
        {
            databaseConnection = null;
            databaseTransaction = null;
        }

        public DBObjectBase(SqlConnection cn, SqlTransaction tn)
        {
            databaseConnection = cn;
            databaseTransaction = tn;
        }

        public DBObjectBase(SqlTransaction tn)
        {
            databaseConnection = tn.Connection;
            databaseTransaction = tn;
        }

        public DBObjectBase(SqlConnection cn)
        {
            databaseConnection = cn;
            databaseTransaction = null;
        }

        /// <summary>
        /// Gets or sets the current SqlConnection (Database context)
        /// </summary>
        public SqlConnection DatabaseConnection
        {
            get { return databaseConnection; }
            set { databaseConnection = value; }
        }

        /// <summary>
        /// Gets or sets the current SqlTransaction (Database context)
        /// </summary>
        public SqlTransaction DatabaseTransaction
        {
            get { return databaseTransaction; }
            set { databaseTransaction = value; }
        }

        protected void CopyMembers(DBObjectBase old)
        {
            this.databaseConnection = old.databaseConnection;
            this.databaseTransaction = old.databaseTransaction;
        }

        protected SqlCommand CreateCommand(string sql)
        {
            // may need a version without transaction
            return new SqlCommand(sql, databaseConnection, databaseTransaction);
        }
    }
}
